import request from '@/utils/request.ts'
import type { ResponseData } from '@/api/type.ts'
import type {
  DoAssignPermissionData,
  Permission,
  PermissionParams,
} from '@/api/acl/permission/type.ts'

enum API {
  PERMISSION_LIST_URL = '/admin/acl/permission',
  PERMISSION_SAVE_URL = '/admin/acl/permission/save',
  PERMISSION_UPDATE_URL = '/admin/acl/permission/update',
  PERMISSION_DELETE_URL = '/admin/acl/permission/remove',
  PERMISSION_ROLE_TO_ASSIGN_URL = '/admin/acl/permission/toAssign',
  PERMISSION_ROLE_DO_ASSIGN_URL = '/admin/acl/permission/doAssignAcl',
}

export const reqPermissionList = () => {
  return request.get<any, ResponseData<Permission[]>>(API.PERMISSION_LIST_URL)
}

export const reqPermissionAddOrUpdate = (permission: PermissionParams) => {
  if (permission?.id) {
    return request.put<any, ResponseData<any>>(
      API.PERMISSION_UPDATE_URL,
      permission,
    )
  } else {
    return request.post<any, ResponseData<any>>(
      API.PERMISSION_SAVE_URL,
      permission,
    )
  }
}

export const reqPermissionDelete = (id: number) => {
  return request.delete<any, ResponseData<any>>(
    API.PERMISSION_DELETE_URL + '/' + id,
  )
}

// 获取某个角色的权限
export const reqPermissionRoleToAssign = (roleId: number) => {
  return request.get<any, ResponseData<Permission[]>>(
    `${API.PERMISSION_ROLE_TO_ASSIGN_URL}/${roleId}`,
  )
}

//给某个角色分配权限
export const reqPermissionRoleDoAssign = (data: DoAssignPermissionData) => {
  return request.post<any, ResponseData<any>>(
    API.PERMISSION_ROLE_DO_ASSIGN_URL,
    data,
  )
}
